
package org.sopt.dev.ImgKnight.monitor.gui.builder;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.sopt.dev.ImgKnight.monitor.Monitor;
import org.sopt.dev.ImgKnight.monitor.dialog.AboutDialog;
import org.sopt.dev.ImgKnight.monitor.resource.ResourceManager;


public class TrayBuilder implements Builder {

	private Monitor monitor = null;

	private MenuItem showItem = null;
	
	private MenuItem hideItem = null;
	
	private MenuItem aboutItem = null;
	
	private MenuItem exitItem = null;
	
	private Tray tray = null;
	
	public TrayBuilder(Monitor monitor) {
		this.monitor = monitor;
		this.tray = monitor.getDisplay().getSystemTray();
	}

	@Override
	public void build() {
		if( tray == null ) {
			MessageBox box = new MessageBox(monitor.getShell(), SWT.ICON_ERROR|SWT.OK);
			box.setText("Error");
			box.setMessage(Monitor.getResourceString("error.Tray.notavailable"));
			box.open();
			return;
		}
		
		final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
		trayItem.setToolTipText("CrawlerMonitor");
		trayItem.setImage(ResourceManager.loadImage(monitor.getDisplay(), "ServiceManager.png"));
		
		Listener menuListener = new Listener() {
			public void handleEvent(Event event) {
				if( event.type == SWT.Selection ) {
					if( event.widget == showItem )
						setVisible(true);
					else if( event.widget == hideItem )
						setVisible(false);
					else if( event.widget == aboutItem ) {
						AboutDialog dialog = new AboutDialog(monitor.getDisplay(), monitor.getShell());
						dialog.open();
					}	
					else if( event.widget == exitItem )
						monitor.getDisplay().dispose();
				}
			}
		};
		
		final Menu menu = new Menu(monitor.getShell(), SWT.POP_UP);
		showItem = new MenuItem(menu, SWT.PUSH);
		showItem.setText("Show");
		showItem.addListener(SWT.Selection, menuListener);
		menu.setDefaultItem(showItem);
		
		hideItem = new MenuItem(menu, SWT.PUSH);
		hideItem.setText("Hide");
		hideItem.addListener(SWT.Selection, menuListener);
		
		@SuppressWarnings("unused")
		MenuItem separator1 = new MenuItem(menu, SWT.SEPARATOR);
		aboutItem = new MenuItem(menu, SWT.PUSH);
		aboutItem.setText("About");
		aboutItem.addListener(SWT.Selection, menuListener);
		
		@SuppressWarnings("unused")
		MenuItem separator2 = new MenuItem(menu, SWT.SEPARATOR);
		exitItem = new MenuItem(menu, SWT.PUSH);
		exitItem.setText("Exit");
		exitItem.addListener(SWT.Selection, menuListener);
		
		Listener itemListener = new Listener() {
			public void handleEvent(Event event) {
				if (event.type == SWT.DefaultSelection) {
					setVisible(true);
				} else if (event.type == SWT.MenuDetect) {
					menu.setVisible(true);
				}
			}
		};
		
		trayItem.addListener(SWT.Show, itemListener);
		trayItem.addListener(SWT.Hide, itemListener);
		trayItem.addListener(SWT.Selection, itemListener);
		trayItem.addListener(SWT.DefaultSelection, itemListener);
		trayItem.addListener(SWT.MenuDetect, itemListener);
		
		monitor.getShell().addShellListener(new ShellAdapter() {
			public void shellIconified(ShellEvent e) {
				if( monitor.isMinToTray() )
					setVisible(false);
			}
		});
	}
	
	private void setVisible(boolean isVisible) {
		monitor.getShell().setVisible(isVisible);
	}
	
}
